package it.wyx.springbootkafkaproducer.service.bsj;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.kafka.clients.CommonClientConfigs;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.config.SaslConfigs;
import org.apache.kafka.common.config.SslConfigs;
import org.apache.kafka.common.security.auth.SecurityProtocol;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;

/**
 * @author wyx
 * @version V 1
 * @Date 2022/12/6
 */
@Configuration
@EnableKafka
public class BsjKafkaConfig {

//  @Value("b-1.cps-kafka-dev.jkqr2l.c2.kafka.cn-north-1.amazonaws.com.cn:9096,b-2.cps-kafka-dev.jkqr2l.c2.kafka.cn-north-1.amazonaws.com.cn:9096")
//  @Value("b-1.cpskafkadevnew.4cmpnj.c2.kafka.cn-north-1.amazonaws.com.cn:9096,b-2.cpskafkadevnew.4cmpnj.c2.kafka.cn-north-1.amazonaws.com.cn:9096")
  @Value("${spring.kafka.bootstrap-servers:}")
  private String bootstrapServers;

  @Value("${spring.kafka.sasl_jaas_config}")
  private String saslJaasConfig;

  @Value("${spring.kafka.sasl-mechanism:}")
  private String saslMechanism;

  @Value("${spring.kafka.truststore-filename:}")
  private String truststoreFilename;

  @Value("${spring.kafka.truststore-password:}")
  private String truststorePassword;




  private static final int FIVE_THOUSAND = 5000;

  @Bean("bsjKafkaTemplate")
  public KafkaTemplate<String, String> bsjKafkaTemplate() {
    return new KafkaTemplate(bsjProducerFactory());
  }

  @Bean
  public ProducerFactory<String, String> bsjProducerFactory() {
    return new DefaultKafkaProducerFactory<>(bsjProducerConfigs());
  }

  @Bean
  public Map<String, Object> bsjProducerConfigs() {
    Map<String, Object> props = new ConcurrentHashMap();
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
    props.put(ProducerConfig.RECONNECT_BACKOFF_MS_CONFIG, 1000);
    props.put(ProducerConfig.RECONNECT_BACKOFF_MAX_MS_CONFIG, FIVE_THOUSAND);
    props.put(ProducerConfig.RETRY_BACKOFF_MS_CONFIG, 1000);
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    props.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, SecurityProtocol.SASL_SSL.toString());
    props.put(SslConfigs.SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG, "");
    props.put(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG, truststoreFilename);
    props.put(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG, truststorePassword);
    props.put(SaslConfigs.SASL_JAAS_CONFIG, saslJaasConfig);
    props.put(SaslConfigs.SASL_MECHANISM, saslMechanism);
    return props;
  }


}
